// page/component/new-pages/user/user.js
const {request} = require('../../../utils/request');
const app = getApp();
Page({
  data:{
    thumb:'',
    nickname:'',
    orders:[],
    hasAddress:false,
    address:{}
  },
  onLoad(){
    var self = this;
    /**
     * 获取用户信息
     */
    wx.getUserInfo({
      success: function(){
        self.setData({
          thumb: "/images/bxd.jpg",
          nickname: app.globalData.userInfo.nickname,
          is_supper: app.globalData.userInfo.roles.includes('superuser')
        })
      }
    }),

    /**
     * 发起请求获取订单列表信息
     */
    request({
      url: '/order',
      method: 'GET',
      showLoading: true
    })
    .then((res) => {
      if (res.code === 2000) {
        this.setData({
          orders: res.data.items
        });
      } else {
        console.error('获取订单失败:', res.message);
      }
    })
    .catch((error) => {
      console.error('请求订单接口出错:', error);
    });
  },
  onShow(){
    var self = this;
    /**
     * 获取本地缓存 地址信息
     */
    wx.getStorage({
      key: 'address',
      success: function(res){
        self.setData({
          hasAddress: true,
          address: res.data
        })
      }
    })
  },
  /**
   * 发起支付请求
   */
  payOrders(){
    wx.requestPayment({
      timeStamp: 'String1',
      nonceStr: 'String2',
      package: 'String3',
      signType: 'MD5',
      paySign: 'String4',
      success: function(res){
        console.log(res)
      },
      fail: function(res) {
        wx.showModal({
          title:'支付提示',
          content:'<text>',
          showCancel: false
        })
      }
    })
  },

  // 检查是否有 superuser 角色
  hasSuperuserRole() {
    console.log(app.globalData.userInfo)
    const userRoles = wx.getStorageSync('userRoles');
    console.log(userRoles)
    return userRoles.includes('superuser');
  },

  // 导航到产品管理页面
  navigateToProductManagement() {
    wx.navigateTo({
      url: '/page/component/productManagement/index'
    });
  },
  getOrder(){
    wx.navigateTo({
      url: '/page/component/orders/orders'
    });
  },
  // 登出方法
  async logout() {
    wx.showLoading({
        title: '登出中...',
    });

    try {
        const res = await request({
            url: '/user/logout',
            method: 'POST',
            showLoading: false
        });

        if (res.code === 2000) {
            // 清除本地存储的 token
            wx.removeStorageSync('token');
            app.globalData.userInfo = null; // 清除全局用户信息
            wx.showToast({
                title: '登出成功',
                icon: 'success'
            });
            // 跳转到登录页面，这里需要根据实际情况修改登录页面路径
            wx.reLaunch({
                url: '/page/component/login/index'
            });
        } else {
            wx.showToast({
                title: '登出失败',
                icon: 'none'
            });
            console.error('登出失败:', res.message);
        }
    } catch (error) {
        wx.showToast({
            title: '请求出错',
            icon: 'none'
        });
        console.error('登出请求出错:', error);
    } finally {
        wx.hideLoading();
    }
}
})